package pl.edu.agh.uhm.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;

import pl.edu.agh.uhm.core.DevicesTreeView;
import pl.edu.agh.uhm.discoverer.Hypervisor;
import pl.edu.agh.uhm.discoverer.VirtualMachine;

import sun.misc.VMSupport;

public class DevicesContentProvider implements ITreeContentProvider{

	
	public DevicesContentProvider(){
		
		
	}
	
	@Override
	public Object[] getChildren(Object arg0) {
		
		if(arg0 instanceof Hypervisor){
			
			Hypervisor hyp=(Hypervisor)arg0;
			Set<String> chlidrenIds=hyp.getVirtualMachineIDs();
			List<VirtualMachine> children=new ArrayList<VirtualMachine>();
			
			for(VirtualMachine vm:DevicesTreeView.vms){
				if(chlidrenIds.contains(vm.getId()))
					children.add(vm);
				
			}
			return children.toArray();
			
		}
		else 
			return new Object[]{};
		
	}

	@Override
	public Object getParent(Object arg0) {
		
	
		if(arg0 instanceof Hypervisor){
			
			return null;
			
		}
		else if(arg0 instanceof VirtualMachine){
			String parentId=((VirtualMachine)arg0).getParentId();
			for(Hypervisor hyp:DevicesTreeView.root){
				if(hyp.getId().equals(parentId))
					return hyp;
			}
			
		}
		
		return null;
		
	}

	@Override
	public boolean hasChildren(Object arg0) {
		
		if(arg0 instanceof Hypervisor){
			
			return true;
			
		}
		
		return false;
	}

	@Override
	public Object[] getElements(Object input) {
		
		List hypervisorsList=(List)input;
		Object[] hypervisors=new Object[hypervisorsList.size()];
		int i=0;
		for(Object o:hypervisorsList){
			hypervisors[i]=o;
			i++;
		}
		return hypervisors;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
		// TODO Auto-generated method stub
		
	}


	
	
}
